package com.yxt.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yxt.model.ChatHis;
import com.yxt.model.UserInfo;
import com.yxt.service.MessageService;
import com.yxt.service.UserService;

import static yxt.util.Util.*;
@Controller
@RequestMapping("user")
public class User {
	@Autowired
	private UserService userService;
	@Autowired
	private MessageService msgService;
	
	@RequestMapping(value="/register.do")
	@ResponseBody 
	public Map<String,Object> addUserInfo(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> paramMap = new HashMap<String,Object>();
		request.setCharacterEncoding("UTF-8");
		Enumeration<String> pNames = request.getParameterNames();
		while(pNames.hasMoreElements()){
			String pName =pNames.nextElement();			
			paramMap.put(pName, request.getParameter(pName));
		}
		//获得用户IP
		String ip = "";
		if (request.getHeader("x-forwarded-for") == null) {
			ip =  request.getRemoteAddr();
		} else {
			ip = request.getHeader("x-forwarded-for");
		}
		paramMap.put("ip", ip);
		Map<String,Object> rtn = new HashMap<String,Object>();
		userService.addUserInfo(paramMap);
		rtn.put("success", true);
		return jsonp(rtn, request, response);
	}
	
	@RequestMapping(value="/login.do")
	@ResponseBody 
	public Map<String,Object> verifyUserInfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Map<String,Object> paramMap = new HashMap<String,Object>();
		Map<String,Object> respMap = new HashMap<String,Object>();
		Enumeration<String> pNames = request.getParameterNames();
		while(pNames.hasMoreElements()){
			String pName =pNames.nextElement();			
			paramMap.put(pName, request.getParameter(pName));
		}
		//获得用户IP
		String ip = "";
		if (request.getHeader("x-forwarded-for") == null) {
			ip =  request.getRemoteAddr();
		} else {
			ip = request.getHeader("x-forwarded-for");
		}
//		paramMap.put("ip", ip);
		boolean success = false;
//		System.out.println(userInfo.getName());
//		return null;
		if(userService.verifyUserInfo(paramMap)) {
			success = true;
			List<ChatHis> chatHisList = msgService.getAllMsgList(paramMap);
			paramMap.put("online", "1");
			userService.setOnline(paramMap);
			respMap.put("chatHisList", chatHisList);
			respMap.put("name", paramMap.get("name"));
			respMap.put("user_id", paramMap.get("user_id"));
		}
		respMap.put("success", success);
		return jsonp(respMap, request, response);
	}
	@RequestMapping(value="/getOnlineList.do")
	@ResponseBody 
	public Map<String,Object> getOnlineListList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> paramMap = new HashMap<String,Object>();
		Map<String,Object> respMap = new HashMap<String,Object>();
		Enumeration<String> pNames = request.getParameterNames();
		
		while(pNames.hasMoreElements()){
			String pName =pNames.nextElement();			
			paramMap.put(pName, request.getParameter(pName));
		}
		List<UserInfo> onlineList = (List<UserInfo>)userService.getOnlineList(paramMap);
		String str1 = "";
		String str2 = "";
		for(UserInfo u: onlineList) {
			str1 += u.getUser_id();
		}
		List<UserInfo> onlineList2 = null;
		String logined = (String)paramMap.get("logined");
		if("true".equals(logined)) {
			do {
				 onlineList2 = (List<UserInfo>)userService.getOnlineList(paramMap);	
				 str2 = "";
				 for(UserInfo u: onlineList2) {
					 str2 += u.getUser_id();
				 }
				 Thread.currentThread().sleep(2000);
			} while(str1.equals(str2));
			respMap.put("onlineList", onlineList2);
		}else {
			respMap.put("onlineList", onlineList);
		}
		
		return jsonp(respMap, request, response);
	}
	
	
	@RequestMapping(value="/logout.do")
	@ResponseBody 
	public void logout(@RequestParam(value = "user_id") String user_id
	) throws Exception{
		Map<String,Object> p = new HashMap<String,Object>();
		p.put("user_id", user_id);
		p.put("online", 0);
		userService.setOnline(p);
	}
	
}
